home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifcico / ifstat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-03  |  4.1 KB  |  196 lines

  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #ifdef HAS_SYSLOG
  10. #include <syslog.h>
  11. #endif
  12. #include "getopt.h"
  13. #include "lutil.h"
  14. #include "xutil.h"
  15. #include "ftn.h"
  16. #include "config.h"
  17. #include "scanout.h"
  18. #include "version.h"
  19.  
  20. void usage(name)
  21. char *name;
  22. {
  23.     confusage("");
  24. }
  25.  
  26. static struct _alist {
  27.     struct _alist *next;
  28.     faddr addr;
  29.     int flavors;
  30.     time_t time;
  31.     off_t size;
  32. } *alist=NULL;
  33.  
  34. #define F_NORMAL 1
  35. #define F_CRASH  2
  36. #define F_HOLD   4
  37. #define F_FREQ   8
  38.  
  39. static int each(faddr*,char,int,char*);
  40.  
  41. int main(argc,argv)
  42. int argc;
  43. char *argv[];
  44. {
  45.     int c,rc;
  46.     struct _alist *tmp;
  47.     char flstr[5];
  48.     time_t age;
  49.     char *unit;
  50.  
  51. #ifdef MAILLOG
  52.     logfacility=MAILLOG;
  53. #endif
  54.  
  55.     setmyname(argv[0]);
  56.     while ((c=getopt(argc,argv,"x:l:h")) != -1)
  57.     if (confopt(c,optarg)) switch (c)
  58.     {
  59.         default:    usage(); exit(1);
  60.     }
  61.  
  62.     if ((rc=readconfig()))
  63.     {
  64.         fprintf(stderr,"Error getting configuration, aborting\n");
  65.         return rc;
  66.     }
  67.  
  68.     if ((rc=scanout(each)))
  69.     {
  70.         fprintf(stderr,"Error scanning outbound, aborting\n");
  71.         return rc;
  72.     }
  73.  
  74.     printf("flavor   size   age\t\taddress\n");
  75.     for (tmp=alist;tmp;tmp=tmp->next)
  76.     {
  77.         strcpy(flstr,"....");
  78.         if ((tmp->flavors) & F_CRASH ) flstr[0]='C';
  79.         if ((tmp->flavors) & F_NORMAL) flstr[1]='N';
  80.         if ((tmp->flavors) & F_HOLD  ) flstr[2]='H';
  81.         if ((tmp->flavors) & F_FREQ  ) flstr[3]='R';
  82.  
  83.         (void)time(&age);
  84.         age-=tmp->time;
  85.         if (age > (30L*24L*60L*60L))
  86.             { age /= (30L*24L*60L*60L); unit="month"; }
  87.         else if (age > (7L*24L*60L*60L))
  88.             { age /= (7L*24L*60L*60L); unit="week"; }
  89.         else if (age > (24L*60L*60L))
  90.             { age /= (24L*60L*60L); unit="day"; }
  91.         else if (age > (60L*60L))
  92.             { age /= (60L*60L); unit="hour"; }
  93.         else if (age > 60L)
  94.             { age /= 60L; unit="minute"; }
  95.         else 
  96.             { unit="second"; }
  97.  
  98.         printf("%s %8lu %3d %s%s   \t%s\n",
  99.             flstr,tmp->size,(int)age,unit,(age==1)?" ":"s",
  100.             ascfnode(&(tmp->addr),0x1f));
  101.     }
  102.  
  103.     return 0;
  104. }
  105.  
  106. static int each(addr,flavor,isflo,fname)
  107. faddr *addr;
  108. char flavor;
  109. int isflo;
  110. char *fname;
  111. {
  112.     struct _alist **tmp;
  113.     struct stat st;
  114.     FILE *fp;
  115.     char buf[256],*p;
  116.  
  117.     for (tmp=&alist;*tmp;tmp=&((*tmp)->next))
  118.         if (((*tmp)->addr.zone == addr->zone) &&
  119.             ((*tmp)->addr.net == addr->net) &&
  120.             ((*tmp)->addr.node == addr->node) &&
  121.             ((*tmp)->addr.point == addr->point) &&
  122.             (((*tmp)->addr.domain == NULL) ||
  123.              (addr->domain == NULL) ||
  124.              (strcasecmp((*tmp)->addr.domain,addr->domain) == 0)))
  125.             break;
  126.     if (*tmp == NULL)
  127.     {
  128.         *tmp=(struct _alist *)xmalloc(sizeof(struct _alist));
  129.         (*tmp)->next=NULL;
  130.         (*tmp)->addr.name=NULL;
  131.         (*tmp)->addr.zone=addr->zone;
  132.         (*tmp)->addr.net=addr->net;
  133.         (*tmp)->addr.node=addr->node;
  134.         (*tmp)->addr.point=addr->point;
  135.         (*tmp)->addr.domain=addr->domain;
  136.         (*tmp)->flavors=0;
  137.         time(&((*tmp)->time));
  138.         (*tmp)->size=0L;
  139.     }
  140.     switch (flavor)
  141.     {
  142.     case 'o':    (*tmp)->flavors |= F_NORMAL; break;
  143.     case 'c':    (*tmp)->flavors |= F_CRASH; break;
  144.     case 'h':    (*tmp)->flavors |= F_HOLD; break;
  145.     case 'r':    (*tmp)->flavors |= F_FREQ; break;
  146.     default:    fprintf(stderr,"Unknown flavor: '%c'\n",flavor); break;
  147.     }
  148.     if (stat(fname,&st) != 0)
  149.     {
  150.         perror("cannot stat");
  151.         st.st_size=0L;
  152.         (void)time(&st.st_mtime);
  153.     }
  154.     if (st.st_mtime < (*tmp)->time) (*tmp)->time = st.st_mtime;
  155.     if (isflo == OUT_FLO)
  156.     {
  157.         if ((fp=fopen(fname,"r")))
  158.         {
  159.             while (fgets(buf,sizeof(buf)-1,fp))
  160.             {
  161.                 if (*(p=buf+strlen(buf)-1) == '\n') *p--='\0';
  162.                 while (isspace(*p)) *p--='\0';
  163.                 for (p=buf;*p && isspace(*p);p++);
  164.                 if (*p == '~') continue;
  165.                 if ((*p == '#') ||
  166.                     (*p == '-') ||
  167.                     (*p == '^') ||
  168.                     (*p == '@')) p++;
  169.                 if (stat(p,&st) != 0)
  170.                 {
  171.                     perror("cannot stat");
  172.                     st.st_size=0L;
  173.                     (void)time(&st.st_mtime);
  174.                 }
  175.                 if ((p=strrchr(fname,'/'))) p++;
  176.                 else p=fname;
  177.                 if ((strlen(p) == 12) &&
  178.                     (strspn(p,"0123456789abcdefABCDEF") == 8) &&
  179.                     (p[8] == '.'))
  180.                 {
  181.                     if (st.st_mtime < (*tmp)->time) 
  182.                         (*tmp)->time = st.st_mtime;
  183.                 }
  184.                 (*tmp)->size += st.st_size;
  185.             }
  186.             fclose(fp);
  187.         }
  188.         else perror("cannot open flo");
  189.     }
  190.     else if (isflo == OUT_PKT)
  191.     {
  192.         if (flavor != 'r') (*tmp)->size += st.st_size;
  193.     }
  194.     return 0;
  195. }
  196.